﻿$(function(){
    $(".news1").fadeTo("fast", 0.8);
    console.log(_departs);
    var departmodel = $(".departmodel").clone();
 	var listDepart = $("#department");
    if(_departs){
        for(var i=0;i<_departs.length;i++){
            var departId = _departs[i].departId;
            var departName = _departs[i].departName;
            var onetotal = _departs[i].onetotal;
            var onenum = _departs[i].onenum;
            var onepercent = Math.round(onenum * 100 / onetotal);
            var twototal = _departs[i].twototal;
            var twonum = _departs[i].twonum;
            var twopercent = Math.round(twonum * 100 / twototal);
			var url = _departs[i].url;
            var onerow = departmodel.clone();
			
			listDepart.append($("<option value='{0}'>{1}</option>".format(departId, departName)));
			
            onerow.attr("departId", departId);
            onerow.children().eq(1).html(departName);//部门名称
			onerow.children().eq(3).html("<a href='{0}' style='font-size:14px' target='_blank'>{0}</a>".format(url));
            onerow.find(":radio").attr("name", departId).each(function(j){
                var id = "{0}_{1}".format(departId, j);
                $(this).attr("id",id);
                $(this).next().attr("for", id);
            });
            

            console.log(i);
            $(".departmodel").last().after(onerow);
        }
        $(".departmodel").each(function(n){
            if(n > 0){
                $(this).removeClass("departmodel");
            }
        });

    }
    $("#btnSubmit").click(function(){
		if(nehnre.queryString()["_"] || getCookie("vote")){
			alert("你已经投过票了!");
			return;
		}
        if($(":radio:checked").size() > 0){
            if($.trim($("#comment").val())){
                if($.trim($("#comment").val()).length < 500){
					setCookie("vote","voted");				
                    $("#formID").submit();
                } else {
                    alert("字数不能超过500字！");
                }
            } else {
				setCookie("vote","voted");				
				$("#formID").submit();			
            }
            
        } else {
            alert("请至少选择一项！");
            $(":radio:checked").eq(0).focus();
        }
    });
    console.log(nehnre.queryString()["_"]);
    if(nehnre.queryString()["_"]){
        alert("投票成功");
    } 
	$("#btnView").click(function(){
		window.open("VoteView.aspx");					 
	});
});
function setCookie(name,value)
{
    var Days = 365; //此 cookie 将被保存一年
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
